package net.proef.sample.controller;

import java.util.List;

import net.proef.sample.domain.Author;
import net.proef.sample.domain.Book;
import net.proef.sample.service.BookService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/authors")
public class AuthorServiceController {

	@Autowired
	BookService bookService;

	@RequestMapping(value = "/")
	public ModelAndView getAllAuthors() {
		List<Author> authors = bookService.getAllAuthors();
		ModelAndView mav = new ModelAndView("xmlView",
				BindingResult.MODEL_KEY_PREFIX + "authors", authors);
		return mav;
	}

	@RequestMapping(value = "/{authorId}")
	public ModelAndView getAuthorById(@PathVariable String authorId) {
		Author author = bookService.getAuthorById(authorId);
		ModelAndView mav = new ModelAndView("xmlView",
				BindingResult.MODEL_KEY_PREFIX + "author", author);
		return mav;
	}

	@RequestMapping(value = "/{authorId}/books")
	public ModelAndView getBooksByAuthor(@PathVariable String authorId) {
		List<Book> books = bookService.getBooksByAuthor(authorId);
		ModelAndView mav = new ModelAndView("xmlView",
				BindingResult.MODEL_KEY_PREFIX + "books", books);
		return mav;
	}
}